// TianguoguoXiaopu-master/pages/sys/bannerManage/addbanner.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    uploaderList: [],
    uploaderNum:0,
    showUpload:true,
    img:"",
    recommendGoods1:[],
    recommendGoodsShow1:[],
    isEnd: false, //到底啦
    items: [
     {
        value:"1",
        title:"商品详情页"
      },
      {value:"2",
      title:"活动页面"}
    ],
    leixing:1,//跳转类型
    checkedGoods:[],
    name:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
   //选择跳转类型
  onChange(e){
    
    this.setData({
      leixing: e.detail.value
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that=this
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    //当前页的options，啥意思呢，就是你可能某个函数需要刷新，但是他的参数正好是传过来的参数
    console.log(currPage.options); 
    console.log(currPage.data.checkedGoods)
    console.log(that.data.checkedGoods)
    that.setData({

    })
  },
  nameInput(e){
    this.data.name = e.detail.value
  },
  //save
  save(){
    if(!this.data.name){
      wx.showModal({
        title: '提示',
        content: '请填写名称',
      })
      
      return
    }
    if(this.data.checkedGoods.length==0){
      wx.showModal({
        title: '提示',
        content: '请添加商品',
      })
      
      return
    
    }
    else if(this.data.uploaderNum  ==0){
      wx.showModal({
        title: '提示',
        content: '请上传图片',
      })
      return
    }
    let goodsid =""
    let goodsids=[]
    if(this.data.leixing==1){

      goodsid =this.data.checkedGoods[0]._id
    }else{
      for (let index = 0; index < this.data.checkedGoods.length; index++) {
        const element = this.data.checkedGoods[index];
        goodsids.push(element._id)
      }

    }
    
    const db = wx.cloud.database()
    db.collection('banners').add({
      data: {
        status:1,
        name:this.data.name,
        type:this.data.leixing,
        pic:this.data.uploaderList[0],
        goodsid:goodsid,
        goodsids:goodsids,
        dateAdd:new Date(),
      },
      success: function(res) {
       
        wx.navigateBack({})
      }
    })
  },
  //跳转选择商品页面
  checkgoods(){
    wx.navigateTo({
      url: 'checkbox_goods?leixing='+this.data.leixing,
    })
  },
  clearImg:function(e){
    var nowList = [];//新数据
    var uploaderList = this.data.uploaderList;//原数据
    
    for (let i = 0; i < uploaderList.length;i++){
        if (i == e.currentTarget.dataset.index){
            continue;
        }else{
            nowList.push(uploaderList[i])
        }
    }
    this.setData({
        uploaderNum: this.data.uploaderNum - 1,
        uploaderList: nowList,
        showUpload: true
    })
  },
  //展示图片
  showImg:function(e){
      var that=this;
      wx.previewImage({
          urls: that.data.uploaderList,
          current: that.data.uploaderList[e.currentTarget.dataset.index]
      })
  },
  //上传图片
  upload: function(e) {
      var that = this;
      wx.chooseImage({
          count:  - that.data.uploaderNum, // 默认9
          sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
          sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
          success: chooseResult => {
          
              wx.cloud.uploadFile({
                  // 指定上传到的云路径
                  cloudPath: (new Date()).valueOf()+'.png', // 文件名
                  // 指定要上传的文件的小程序临时文件路径
                  filePath: chooseResult.tempFilePaths[0],
                  // 成功回调
                  success: res => {
                    console.log('上传成功', res)
                    // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                    let tempFilePaths = res.fileID;
                  
                    wx.cloud.getTempFileURL({
                      fileList:[{
                          fileID:tempFilePaths,
                          maxAge:60*60


                      }]
                    }).then((res)=>{
                    
                      let uploaderList = that.data.uploaderList.concat(res.fileList[0].tempFileURL);
                    
                      if (uploaderList.length==1){
                        that.setData({
                            showUpload:false
                        })
                      }
                      that.setData({
                        uploaderList: uploaderList,
                        uploaderNum: uploaderList.length,
                      })
                    })
                    that.setData({
                      
                    })
                  },
                })
              
          }
      })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})